﻿using System.Data;
using OVS.CoreDomain;
using OVS.CoreDomain.Data;
using System.Collections.Generic;
using System;

namespace OVS.Data
{
    public class DalElection : Dal<IElection, Election>, IDalElection
    {
        private const string SP_ADD_ELECTION = "usp_AddElection";
        private const string SP_GET_ELECTIONS = "usp_GetElections";
        private const string SP_GET_ELECTION = "usp_GetElection";
        private const string SP_UPDATE_ELECTION = "usp_UpdateElection";






        private readonly IDalManager _manager;
        private static readonly IDalElection _instance = new DalElection();

        public static IDalElection Instance
        {
            get { return _instance; }
        }

        public DalElection()
        {
            _manager = new DalManager();
        }

        public DalElection(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IElection obj, DataRow row)
        {
            obj.ElectionId = _manager.GetValue<int>(row, "ElectionId");
            obj.ElectionDescription = _manager.GetValue<string>(row, "ElectionDescription");
            obj.ElectionDate = _manager.GetValue<DateTime>(row, "ElectionDate");
            obj.ResultDate = _manager.GetValue<DateTime>(row, "ElectionResultDate");

        }

        public bool AddElection(IElection election)
        {
            _manager.StoredProcedureName = SP_ADD_ELECTION;

            _manager.AddInParameter("@ElectionDescription", DbType.String, election.ElectionDescription);
            _manager.AddInParameter("@ElectionDate", DbType.DateTime, election.ElectionDate);
            _manager.AddInParameter("@ElectionResultDate", DbType.DateTime, election.ResultDate);

            _manager.ExecuteNonQuery();

            return true;

        }




        public IList<IElection> GetElections()
        {
            _manager.StoredProcedureName = SP_GET_ELECTIONS;

            List<IElection> elections = new List<IElection>();
            foreach (IElection election in ConvertAll(_manager.ExecuteDataSet()))
                elections.Add(election);



            return elections;
        }

        public IElection GetElection(int electionId)
        {
            _manager.StoredProcedureName = SP_GET_ELECTION;
            _manager.AddInParameter("@ElectionId", DbType.String, electionId);
            IElection election = Convert(_manager.ExecuteDataSet());
            return election;
        }



        #region IDalElection Members


        public bool UpdateElection(IElection election)
        {
            _manager.StoredProcedureName = SP_UPDATE_ELECTION;

            _manager.AddInParameter("@ElectionId", DbType.Int32, election.ElectionId);
            _manager.AddInParameter("@ElectionDescription", DbType.String, election.ElectionDescription);
            _manager.AddInParameter("@ElectionDate", DbType.DateTime, election.ElectionDate);
            _manager.AddInParameter("@ElectionResultDate", DbType.DateTime, election.ResultDate);         
            _manager.ExecuteNonQuery();

            return true;
        }

        #endregion
    }
}
